home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10602 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  88 lines

  1. Path: info-server.bbn.com!news
  2. From: vanegas@jade.bbn.com (Rodrigo Vanegas)
  3. Newsgroups: comp.lang.c++
  4. Subject: Implementing a filtered iostream
  5. Date: 08 Mar 1996 15:27:11 -0500
  6. Organization: BBN
  7. Sender: vanegas@jade.bbn.com
  8. Message-ID: <kd4g2bjxrds.fsf@jade.bbn.com>
  9. Reply-To: vanegas@bbn.com
  10. NNTP-Posting-Host: jade.bbn.com
  11. X-Newsreader: Gnus v5.0.15
  12.  
  13. How would I implement a subclass of iostream such that all data going
  14. through the IO would be filtered somehow.  I imagine something like
  15. the following.
  16.  
  17.  
  18.   class rot13stream : public fstream 
  19.   {
  20.       // magic goes here!
  21.   };
  22.  
  23.   main()
  24.   {
  25.       String m;
  26.  
  27.       fstream f;
  28.       f.open("message");
  29.       f << "Gur chapuyvar!" << endl;
  30.       f.close();
  31.  
  32.       rot13stream r;
  33.       r.open("message");
  34.       m.getLine(r);
  35.  
  36.       cout << m;
  37.   }
  38.  
  39.  
  40.   sh$ run
  41.   The punchline!
  42.  
  43.  
  44. Even better would be a way to implement a two-way string stream of
  45. sorts implementing a filter.
  46.  
  47.  
  48.   class filterStream : public strstream // maybe?
  49.   {
  50.       // some magic.
  51.   }
  52.  
  53.   class rot13filterStream : public filterStream
  54.   {
  55.       // more magic!
  56.   }
  57.  
  58.   main()
  59.   {
  60.       String message;
  61.  
  62.       rot13filterStream filt;
  63.       filt << "Gur vachg pbhyq pbzr va puhaxf" << endl;
  64.       filt << "naq or eryrnfrq";
  65.  
  66.       message.getLine(filt);
  67.       cout << "1: " << message << endl;
  68.  
  69.       filt << "va puhaxf nf jryy..." << endl;
  70.       message = "";
  71.       message.getLine(filt);
  72.  
  73.       cout << "2: " << message << endl;
  74.   }
  75.  
  76.  
  77.   sh$ run
  78.   1: The input could come in chunks
  79.   2: and be released in chunks as well...
  80.  
  81.  
  82. How about it?  Can the standard C++ IO library handle this?  I'd like
  83. to be able to take advantage of all those methods defined for
  84. iostream...
  85.  
  86. -- 
  87. Rodrigo <Vanegas@bbn.com>
  88.